import { Component, Host, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { AppComponent } from '../../../../app.component';
import { Subscription } from 'rxjs';
import { CompanyService } from '../company.service';
import { CompanyModel } from '../company.model';
import { CompanyEsgService } from './companyEsg.service';
import { ResearchState } from '../../research.service';
import { EsgThemeOverallPerformanceSummary } from './esgIssue/models/esgThemeOverallPerformanceSummary.model';
import * as _ from 'lodash';
import { CompanyReportDownloadModel } from '../shared/companyHeader/companyReportDownload/companyReportDownload.model';
import { AnalyticsService } from '../../../../shared/services/googleAnalytics.service';
import { ReportAnalytics } from '../../../../shared/models/googleAnalytics.model';
import { CompanyReportDownloadComponent } from '../shared/companyHeader/companyReportDownload/companyReportDownload.component';
import { ModalComponent } from '../../../../shared/components/modal/modal.component';
import { StorageService } from '../../../../shared/services/storage.service';

/**
 * Company ESG main component
 *
 * Author: Sorin Spiru
 * Date: 13.01.2018
 */
@Component({
    moduleId: module.id,
    templateUrl: 'companyEsg.component.html',
    providers: [CompanyEsgService]
})
export class CompanyEsgComponent {
    private sub: Subscription;
    public company: CompanyModel;
    public esgThemeOverallPerformanceSummary: EsgThemeOverallPerformanceSummary;
    public noResearchMessage: string;
    public researchType;
    public downloadData: CompanyReportDownloadModel;
    public analyticsData: ReportAnalytics = new ReportAnalytics('Research', 'PDFESGRatingReportFull default settings');
    public summaryAnalyticsData: ReportAnalytics = new ReportAnalytics('Research', 'PDFESGRatingReportSummary default settings');
    public weightMatrices: any[] = [{
        id: "00000000-0000-0000-0000-000000000000",
        name: "Default Weight Matrix"
    }];
    public referenceUniverses: any[] = [];

    public hasCustomDownloadSettings: boolean;
    public selectedMatrix: string = '00000000-0000-0000-0000-000000000000';
    public selectedUniverse: string;
    public selectedGroup: number = 0;
    public selectedCombineType: number = 16;
    public selectedCombineTemplate: number = 2;
    @ViewChild('esgReportTask') esgReportTask: CompanyReportDownloadComponent;
    @ViewChild('customSettingsModal') customSettingsModal: ModalComponent;

    constructor(
        private route: ActivatedRoute,
        public companyService: CompanyService,
        private esgService: CompanyEsgService,
        private analyticsService: AnalyticsService,
        public router: Router,
        @Host() private rootComponent: AppComponent) {
    }

    ngOnInit() {
        this.sub = this.companyService.company.subscribe(data => {
            this.company = data;
            this.researchType = this.company.comprehensiveEsgResearchState === ResearchState.ResearchAvailable ? 'comprehensive' : 'core';

            // set download report data
            this.downloadData = this.companyService.getEsgDownloadData(this.company);

            if (this.researchType === 'comprehensive') {
                this.companyService.menuItems.next(this.route.routeConfig.children);
            } else {
                const routeChildren = _.filter(this.route.routeConfig.children, (item) => {
                    return ['environment', 'social', 'governance'].indexOf(item.path) === -1;
                });
                this.companyService.menuItems.next(routeChildren);
            }

            this.rootComponent.finishLoading();
            
            this.hasCustomDownloadSettings = StorageService.get().userInfo.accountTier != 3 && this.company.coreEsgResearchState != ResearchState.ResearchAvailable;
            if(this.hasCustomDownloadSettings)
            {
                //get matrices
                this.esgService.getWeightMatrices().subscribe(data => {
                    this.weightMatrices = this.weightMatrices.concat(data.map(function (o) { return { id: o.id, name: o.name } }));
                })
                //get reference universes
                this.esgService.getReferenceUniverses().subscribe(data => {
                    this.referenceUniverses = data;
                    this.selectedUniverse = data[0].id;
                });
            }
        });
    }    

    ngOnDestroy() {
        this.sub.unsubscribe();
    }

    public customReportGeneration() {
        let standardAction = this.analyticsData.action;
        this.analyticsData.action = "PDFESGRatingReport custom settings";
        this.esgReportTask.generateReport({
            matrixId: this.selectedMatrix,
            referenceUniverseId: this.selectedUniverse,
            comparativeGroup: this.selectedGroup,
            companyType: this.selectedCombineType,
            templateType: this.selectedCombineTemplate,
            matrixName: this.weightMatrices.find(x => x.id == this.selectedMatrix).name,
            referenceUniverseName: this.referenceUniverses.find(x => x.id == this.selectedUniverse).name,
            comparativeGroupName: this.selectedGroup == 0 ? 'Peer Group' : 'Industry Group'
        });
        this.analyticsData.action = standardAction;
        this.customSettingsModal.hide();
    }
}